#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MCPy 基础使用示例

本示例展示如何使用 MCPy 编译器将 Python 代码编译为 C++ 代码。
包括基本的函数定义、类型注解、内建函数使用等。
"""
from mcpy.decorators import export, main
from typing import Any

from gtest import EXPECT_EQ
from precise_types import run_all_precise_type_tests
from str_format.fstring import *


def add_numbers(a: int, b: int) -> int:
    """加法函数示例"""
    return a + b


def multiply_numbers(x: int, y: int) -> int:
    """乘法函数示例"""
    result = x * y
    return result


def process_string(text: str) -> int:
    """字符串处理示例"""
    length: int = len(text)
    return length


@staticmethod
def process_list(items: list[int]) -> int:
    """列表处理示例"""
    total: int = 0
    for item in items:
        total += item
    return total


@staticmethod
def fibonacci(n: int) -> int:
    """斐波那契数列示例"""
    if n <= 1:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)


@export()
def factorial(n: int) -> int:
    """阶乘函数示例"""
    if n <= 1:
        return 1
    result: int = 1
    i: int = 2
    while i <= n:
        result *= i
        i += 1
    return result


@export()
class BasicUsage:
    _value: int = 0

    @staticmethod
    def do_something_static() -> int:
        return 0

    def do_something(self) -> int:
        self._value += 1
        return self._value


@main()
def _main() -> int:
    """主函数示例 - 使用结构化测试"""
    print("开始运行 MCPy 基础使用测试...")

    # 基本运算测试
    print("\n=== 基本运算测试 ===")
    sum_result = add_numbers(10, 20)
    EXPECT_EQ(sum_result, 30)

    product_result = multiply_numbers(5, 6)
    EXPECT_EQ(product_result, 30)

    # 字符串处理测试
    print("\n=== 字符串处理测试 ===")
    text_length = process_string("Hello, MCPy!")
    EXPECT_EQ(text_length, 12)

    # 列表处理测试
    print("\n=== 列表处理测试 ===")
    numbers: list[int] = [1, 2, 3, 4, 5]
    list_sum = process_list(numbers)
    EXPECT_EQ(list_sum, 15)

    # 递归函数测试
    print("\n=== 递归函数测试 ===")
    fib_result = fibonacci(10)
    EXPECT_EQ(fib_result, 55)

    fact_result = factorial(5)
    EXPECT_EQ(fact_result, 120)

    # 类型测试
    print("\n=== 类型测试 ===")
    any_val: Any = 1
    EXPECT_EQ(any_val + 1, 2)

    set_val: set = set({1, 2, 3})
    # EXPECT_TRUE(2 in set_val)  # TODO: 修复 'in' 操作符的 C++ 代码生成

    # 格式化测试
    print("\n=== 格式化测试 ===")
    fstring_basic_result = test_fstring_basic()
    expected_fstring = "Hello MCPy, you are 5 years old!"
    EXPECT_EQ(fstring_basic_result, expected_fstring)

    fstring_expr_result = test_fstring_expressions()
    expected_expr = "Sum: 30, Product: 200"
    EXPECT_EQ(fstring_expr_result, expected_expr)

    fstring_format_result = test_fstring_format_spec()
    expected_format = "Pi with 2 decimals: 3.14"
    EXPECT_EQ(fstring_format_result, expected_format)

    # 类测试
    print("\n=== 类测试 ===")
    bu: BasicUsage = BasicUsage()
    first_call = bu.do_something()
    EXPECT_EQ(first_call, 1)

    second_call = bu.do_something()
    EXPECT_EQ(second_call, 2)

    static_result = bu.do_something_static()
    EXPECT_EQ(static_result, 0)

    # 精确整数类型测试
    print("\n=== 精确整数类型测试 ===")
    run_all_precise_type_tests()

    print("\n所有测试完成！")
    return 0
